﻿using System;
using System.ComponentModel;
using SedionFormDesigner.Designer;
using System.Diagnostics;

namespace SedionFormDesigner.Infrastructure
{
    /// <summary>
    ///  保存组件集合, 与host的sites集合内的组件平行
    /// </summary>
    public class DesignerComponentCollection : ComponentCollection
    {
        private SedionDesignerHost host;

        internal DesignerComponentCollection(SedionDesignerHost host)
            : base(new IComponent[0])
        {
            this.host = host;

            // 首先先填充site数据列表,之后在更新
            if (host.Sites != null)
            {
                foreach (ISite site in host.Sites.Values)
                {
                    InnerList.Add(site.Component);
                }
            }
        }

        /// <summary>
        ///  用名称通过Sites访问组件
        /// </summary>
        public override IComponent this[string name]
        {
            get
            {
                Debug.Assert(name != null, "name is null");
                if (name == null)
                    return null;

                if (name.Length == 0)
                {
                    Debug.Assert(host.RootComponent != null, "base component is null");
                    return host.RootComponent;
                }

                ISite site = (ISite)host.Sites[name];
                return (site == null) ? null : site.Component;
            }
        }

        internal void Add(ISite site)
        {
            InnerList.Add(site.Component);
        }

        internal void Clear()
        {
            InnerList.Clear();
        }

        internal void Remove(ISite site)
        {
            InnerList.Remove(site.Component);
        }
    }
}
